//
//  NetworkStateUtils.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/6/23.
//

#import "NetworkStateUtils.h"

#import "Reachability.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

#import "AFNetworkReachabilityManager.h"

@implementation NetworkStateUtils

#pragma mark - 单次当前网络状态监测

+ (NSString *)networkStatus {
    Reachability *reachability   = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    NSString *net = @"WIFI";
    switch (internetStatus) {
        case ReachableViaWiFi:
            net = @"WIFI";
            break;
        case ReachableViaWWAN:
            net = [self getNetType];   //判断具体无线类型
            break;
        case NotReachable:
            net = @"当前无网路连接";
        default:
            break;
    }
    return net;
}

// 如需精确的网络状态可以根据以下详细的类型，做进一步的判断

// == 2G ==
// CTRadioAccessTechnologyCDMA1x        2G
// CTRadioAccessTechnologyGPRS          GPRS
// CTRadioAccessTechnologyEdge          2.75G EDGE
// == 3G ==
// CTRadioAccessTechnologyCDMAEVDORev0  3G
// CTRadioAccessTechnologyCDMAEVDORevA  3G
// CTRadioAccessTechnologyCDMAEVDORevB  3G
// CTRadioAccessTechnologyWCDMA         3G
// CTRadioAccessTechnologyHSDPA         3.5G HSDPA
// CTRadioAccessTechnologyHSUPA         3.5G HSUPA
// CTRadioAccessTechnologyeHRPD         HRPD
// == 4G ==
// CTRadioAccessTechnologyLTE           4G
// == 5G ==
// CTRadioAccessTechnologyNRNSA         5G NSA
// CTRadioAccessTechnologyNR            5G

+ (NSString *)getNetType
{
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    /// 注意：没有SIM卡，值为空
    NSString *currentStatus;
    NSString *currentNet = @"5G";
    if (@available(iOS 12.1, *)) {
        if (info && [info respondsToSelector:@selector(serviceCurrentRadioAccessTechnology)]) {
            NSDictionary *radioDic = [info serviceCurrentRadioAccessTechnology];
            if (radioDic.allKeys.count) {
                currentStatus = [radioDic objectForKey:radioDic.allKeys[0]];
            }
        }
    }else{
        NSDictionary *radioDic = [info serviceCurrentRadioAccessTechnology];
        currentStatus = [radioDic objectForKey:radioDic.allKeys[0]];
    }
    
    if ([currentStatus isEqualToString:CTRadioAccessTechnologyCDMA1x] ||
        [currentStatus isEqualToString:CTRadioAccessTechnologyGPRS] ||
        [currentStatus isEqualToString:CTRadioAccessTechnologyEdge]) {
        currentNet = @"2G";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyWCDMA] ||
              [currentStatus isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0] ||
              [currentStatus isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA] ||
              [currentStatus isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB] ||
              [currentStatus isEqualToString:CTRadioAccessTechnologyeHRPD] ||
              [currentStatus isEqualToString:CTRadioAccessTechnologyHSDPA] ||
              [currentStatus isEqualToString:CTRadioAccessTechnologyHSUPA]){
        currentNet = @"3G";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyLTE]){
        currentNet = @"4G";
    }else if (@available(iOS 14.1, *)) {
        if ([currentStatus isEqualToString:CTRadioAccessTechnologyNRNSA]){
            currentNet = @"5G NSA";
        }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyNR]){
            currentNet = @"5G";
        }
    }
    return currentNet;
}


#pragma mark - AFN 网络监测（未完成）

- (void)afnReachabilityTest {
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 一共有四种状态
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"AFNetworkReachability Not Reachable");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"AFNetworkReachability Reachable via WWAN");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"AFNetworkReachability Reachable via WiFi");
                break;
            case AFNetworkReachabilityStatusUnknown:
            default:
                NSLog(@"AFNetworkReachability Unknown");
                break;
        }
    }];
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

#pragma mark - Reachability 持续网络监测（未完成）

/**
 * 持续监听
 * Reachability 可以使用通知的方式监听网络变化，在某类中注册通知方法，开启监测。
*/

//@property (nonatomic, strong) Reachability *reachability;

//NSString *hostName = @"www.baidu.com";
//self.reachability = [Reachability reachabilityWithHostName:hostName];
//[self.hostReachability startNotifier];

//[[NSNotificationCenter defaultCenter] addObserver:self
//                                         selector:@selector(appReachabilityChanged:)
//                                             name:kReachabilityChangedNotification
//                                           object:nil];

//- (void)appReachabilityChanged:(NSNotification *)notification{
//    Reachability *reach = [notification object];
//    if([reach isKindOfClass:[Reachability class]]){
//        NetworkStatus status = [reach currentReachabilityStatus];
//        if (reach == self.reachability) {
//            if (status == NotReachable) {
//                NSLog(@"routerReachability NotReachable");
//            } else if (status == ReachableViaWiFi) {
//                NSLog(@"routerReachability ReachableViaWiFi");
//            } else if (status == ReachableViaWWAN) {
//                NSLog(@"routerReachability ReachableViaWWAN");
//            }
//        }
//    }
//}

//- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
//}


@end
